import {HIDDEN_SELECTED_ELEM_ID} from "~utils/constants";

const initializeHighlightImage = () => {
  const allImgs = document.getElementsByTagName('img');
  if (!document.getElementById(HIDDEN_SELECTED_ELEM_ID)) {
    let hiddenSpanElem = document.createElement("span");
    hiddenSpanElem.id = HIDDEN_SELECTED_ELEM_ID;
    hiddenSpanElem.hidden = true;
    document.body.appendChild(hiddenSpanElem);
  }
  function getURL(e) {
    let hiddenSpanElem = document.getElementById(HIDDEN_SELECTED_ELEM_ID);
    if (!hiddenSpanElem) {
      console.warn(`hidden right clicked element not found!`);
    }
    hiddenSpanElem.innerText = this.src;
  }

  Array.from(allImgs).forEach(img => {
    img.addEventListener('contextmenu', getURL);
    console.log(`contextmenu listener attached for ${img.src}`);
  })
}

export default initializeHighlightImage;
